const express = require("express")
const {
    json
} = require("sequelize/dist")
const app = express()
const Houses = require('../service/houseService')



app.get("/houses", async (req, res) => {
    console.log(req.query);
    let result = await Houses.getHouses(req.query.pageNum,req.query.pageSize)
    console.log(result);
    res.send(result)
})


app.post("/houses/:id", async (req, res) => {
    console.log(req.body);
    let result = await Houses.addHouses(req.body)
    console.log(result);
})
app.post("/addhouse", async (req, res) => {
    let result = await Houses.addHouses(req.body.description,req.body.price,req.body.floor,req.body.areaId,req.body.houseCode)
    // console.log(req.body);
    res.send(result)
})

app.delete("/deletehouse", async (req, res) => {
    let result = await Houses.deleteHouses(req.query.id)
    // console.log(result);
    res.send(result)
})

// app.get("/updatehouses", async (req, res) => {
//     console.log(req.query);
//     let result = await Houses.updateHouses(req.query.price, req.query.id)
//     console.log(result);
//     res.send(result)
// <<<<<<< HEAD
// })


// app.get("/updatehouses", async (req, res) => {
//     // console.log(req.query);
//     let result = await Houses.updateHouses(req.query.city)
//     // console.log(result);
//     res.send(result)
// })


// =======
// })


app.get("/updatehouses", async (req, res) => {
    console.log(req.query);
    let result = await Houses.updateHouses(req.query.description,req.query.price,req.query.floor,req.query.areaId,req.query.houseCode,req.query.id)
    // console.log(result);
    res.send(result)
})



app.get("/selecthouses", async (req, res) => {
    console.log(req.query);

    // console.log(JSON.parse(req.query.city));

    req.query.city = req.query.city ? req.query.city : '';
    req.query.type = req.query.type ? req.query.type : '';
    req.query.tese1 = req.query.tese1 ? req.query.tese1 : '';
    req.query.tese2 = req.query.tese2 ? req.query.tese2 : '';
    req.query.tese3 = req.query.tese3 ? req.query.tese3 : '';
    let result = await Houses.selectHouses(req.query.city, req.query.type, req.query.tese1, req.query.tese2, req.query.tese3)
    // console.log(result);
    res.end(JSON.stringify(result))
})

// 根据租金发送请求
// app.get('/zhujin', async (req, res) => {
//     console.log(req.query);
//     let result = await Houses.selectHousesByZhujin({
//         city: req.query.city,
//         min: req.query.min,
//         max: req.query.max
//     })
//     res.end(JSON.stringify(result))
// })
// 筛选房源
app.get('/shaixuan', async (req, res) => {
    console.log(req.query);
    let result = await Houses.selectHousesByShaixuan({
        city: req.query.city,
        min: req.query.min,
        max: req.query.max,
        roomType: req.query.roomType,
        floor:req.query.floor,
        community: req.query.community,
        roomTypeID: req.query.roomTypeID,
        lineNum:req.query.lineNum,
        lineId:req.query.lineId,
        oriented:req.query.oriented,
        tags:req.query.tags,
        communityID:req.query.communityID,
        tagsId:req.query.tagsId,
        size:req.query.size,
        geduan:req.query.geduan,
        year:req.query.year
    })
    res.end(JSON.stringify(result))
})

app.get("/houses/id", async (req, res) => {
    // console.log(req.query);
    let result = await Houses.getHousesById(req.query.id)
    console.log(result);
    res.send(result)
})
module.exports = app;